package com.dayuanit.dy15.atm.service.impl;

import com.dayuanit.dy15.atm.dto.CardDTO;
import com.dayuanit.dy15.atm.entity.AtmCard;
import com.dayuanit.dy15.atm.exception.BizException;
import com.dayuanit.dy15.atm.mapper.AtmCardMapper;
import com.dayuanit.dy15.atm.service.AtmCardService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * (AtmCard)表服务实现类
 *
 * @author makejava
 * @since 2020-05-23 11:48:32
 */
@Service("atmCardService")
public class AtmCardServiceImpl implements AtmCardService {
    @Resource
    private AtmCardMapper atmCardMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public AtmCard queryById(Long id) {
        return this.atmCardMapper.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<AtmCard> queryAllByLimit(int offset, int limit) {
        return this.atmCardMapper.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param atmCard 实例对象
     * @return 实例对象
     */
    @Override
    public AtmCard insert(AtmCard atmCard) {
        this.atmCardMapper.insert(atmCard);
        return atmCard;
    }

    /**
     * 修改数据
     *
     * @param atmCard 实例对象
     * @return 实例对象
     */
    @Override
    public AtmCard update(AtmCard atmCard) {
        this.atmCardMapper.update(atmCard);
        return this.queryById(atmCard.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.atmCardMapper.deleteById(id) > 0;
    }

    /**
     * 查询名下银行卡
     * @param userId 唯一id
     * @return
     */
    @Override
    public List<CardDTO> listBankCards(long userId) {
        //没有银行卡就返回空的list集合
        List<AtmCard> cardList = atmCardMapper.listCardByUserId(userId);
        if (cardList.isEmpty()){
            //public static final List EMPTY_LIST = new EmptyList<>();
            return Collections.EMPTY_LIST;
        }
        //银行卡隐私信息不能展示 返回可展示Carddtos
        List<CardDTO> cardDtos = new ArrayList<>(cardList.size());
        for (AtmCard atmCard : cardList) {
            CardDTO cardDTO = new CardDTO();
            BeanUtils.copyProperties(atmCard,cardDtos);
            cardDtos.add(cardDTO);
        }

        return cardDtos;
    }
}
